//
// Created by postgres on 4/9/17.
//
#include <stddef.h>
#include <string.h>
#include <ctype.h>
char *str_split( char **str, char *delimiters ) {
    char *head, *tail, *rv;
    if ( *str == NULL || **str == '\0' )
        rv = NULL;
    else if ( delimiters == NULL || *delimiters == '\0' )
        rv = NULL;
    else {
        rv = head = *str;
        if ( ( tail = strpbrk( head, delimiters ) ) == NULL )
            *str = head + strlen( head );
        else {
            *tail = '\0';
            *str = tail + 1;
        }
    }
    return rv;

}
#include <assert.h>
#include <stdio.h>

int str_split_demo( void ) {

    char *wrk, *rv;
    char delimiters[] = ":;";
    char wrkstr[] = "1:2::4:;6:7";

    wrk = "";
    assert( str_split( NULL, delimiters ) == NULL );
    assert( str_split( &wrk, delimiters ) == NULL );

    wrk = wrkstr;
    assert( str_split( &wrk, NULL ) == NULL );
    assert( str_split( &wrk, "" ) == NULL );

    while((rv = str_split( &wrk, delimiters )))
        puts( rv );

    return 0;
}
char * myStrtok;
//（全局变量）

char * strtokx( char * s,const char * ct) {

    char *sbegin, *send;
    sbegin = s ? s : myStrtok;//
    if (!sbegin) {
        return NULL;//结尾
    }
    sbegin += strspn(sbegin, ct);//返回字符串中第一个不在指定字符串中出现的字符下标
    if (*sbegin == '\0') {
        myStrtok = NULL;
        return (NULL);
    }
    send = strpbrk(sbegin, ct);
    if (send && *send != '\0')
        *send++ = '\0';
    myStrtok = send;
    return (sbegin);

}
char *ltrim(char *s)
{
    while(isspace(*s)) s++;
    return s;
}

char *rtrim(char *s)
{
    char* back = s + strlen(s);
    while(isspace(*--back));
    *(back+1) = '\0';
    return s;
}

char *trim(char *s)
{
    return rtrim(ltrim(s));
}

